import { HashMap } from '@kit.ArkTS';
import { KeyCode } from '@kit.InputKit';
import { ParsecKeycode } from './Enums';

/**
 * 日期：2025/7/19
 */
export class CodeMap {
  private static _instance: CodeMap
  private keyCodeMap: HashMap<KeyCode, ParsecKeycode> = new HashMap();

  private constructor() {
    this.initKeyCode()
  }

  private static get instance(): CodeMap {
    return this._instance ?? new CodeMap()
  }

  private initKeyCode() {
    this.keyCodeMap.set(KeyCode.KEYCODE_A, ParsecKeycode.KEY_A)
    this.keyCodeMap.set(KeyCode.KEYCODE_B, ParsecKeycode.KEY_B)
    this.keyCodeMap.set(KeyCode.KEYCODE_C, ParsecKeycode.KEY_C)
    this.keyCodeMap.set(KeyCode.KEYCODE_D, ParsecKeycode.KEY_D)
    this.keyCodeMap.set(KeyCode.KEYCODE_E, ParsecKeycode.KEY_E)
    this.keyCodeMap.set(KeyCode.KEYCODE_F, ParsecKeycode.KEY_F)
    this.keyCodeMap.set(KeyCode.KEYCODE_G, ParsecKeycode.KEY_G)
    this.keyCodeMap.set(KeyCode.KEYCODE_H, ParsecKeycode.KEY_H)
    this.keyCodeMap.set(KeyCode.KEYCODE_I, ParsecKeycode.KEY_I)
    this.keyCodeMap.set(KeyCode.KEYCODE_J, ParsecKeycode.KEY_J)
    this.keyCodeMap.set(KeyCode.KEYCODE_K, ParsecKeycode.KEY_K)
    this.keyCodeMap.set(KeyCode.KEYCODE_L, ParsecKeycode.KEY_L)
    this.keyCodeMap.set(KeyCode.KEYCODE_M, ParsecKeycode.KEY_M)
    this.keyCodeMap.set(KeyCode.KEYCODE_N, ParsecKeycode.KEY_N)
    this.keyCodeMap.set(KeyCode.KEYCODE_O, ParsecKeycode.KEY_O)
    this.keyCodeMap.set(KeyCode.KEYCODE_P, ParsecKeycode.KEY_P)
    this.keyCodeMap.set(KeyCode.KEYCODE_Q, ParsecKeycode.KEY_Q)
    this.keyCodeMap.set(KeyCode.KEYCODE_R, ParsecKeycode.KEY_R)
    this.keyCodeMap.set(KeyCode.KEYCODE_S, ParsecKeycode.KEY_S)
    this.keyCodeMap.set(KeyCode.KEYCODE_T, ParsecKeycode.KEY_T)
    this.keyCodeMap.set(KeyCode.KEYCODE_U, ParsecKeycode.KEY_U)
    this.keyCodeMap.set(KeyCode.KEYCODE_V, ParsecKeycode.KEY_V)
    this.keyCodeMap.set(KeyCode.KEYCODE_W, ParsecKeycode.KEY_W)
    this.keyCodeMap.set(KeyCode.KEYCODE_X, ParsecKeycode.KEY_X)
    this.keyCodeMap.set(KeyCode.KEYCODE_Y, ParsecKeycode.KEY_Y)
    this.keyCodeMap.set(KeyCode.KEYCODE_Z, ParsecKeycode.KEY_Z)

    this.keyCodeMap.set(KeyCode.KEYCODE_0, ParsecKeycode.KEY_0)
    this.keyCodeMap.set(KeyCode.KEYCODE_1, ParsecKeycode.KEY_1)
    this.keyCodeMap.set(KeyCode.KEYCODE_2, ParsecKeycode.KEY_2)
    this.keyCodeMap.set(KeyCode.KEYCODE_3, ParsecKeycode.KEY_3)
    this.keyCodeMap.set(KeyCode.KEYCODE_4, ParsecKeycode.KEY_4)
    this.keyCodeMap.set(KeyCode.KEYCODE_5, ParsecKeycode.KEY_5)
    this.keyCodeMap.set(KeyCode.KEYCODE_6, ParsecKeycode.KEY_6)
    this.keyCodeMap.set(KeyCode.KEYCODE_7, ParsecKeycode.KEY_7)
    this.keyCodeMap.set(KeyCode.KEYCODE_8, ParsecKeycode.KEY_8)
    this.keyCodeMap.set(KeyCode.KEYCODE_9, ParsecKeycode.KEY_9)

    this.keyCodeMap.set(KeyCode.KEYCODE_ENTER, ParsecKeycode.KEY_ENTER)
    this.keyCodeMap.set(KeyCode.KEYCODE_ESCAPE, ParsecKeycode.KEY_ESCAPE)
    this.keyCodeMap.set(KeyCode.KEYCODE_TAB, ParsecKeycode.KEY_TAB)
    this.keyCodeMap.set(KeyCode.KEYCODE_SPACE, ParsecKeycode.KEY_SPACE)
    this.keyCodeMap.set(KeyCode.KEYCODE_MINUS, ParsecKeycode.KEY_MINUS)
    this.keyCodeMap.set(KeyCode.KEYCODE_EQUALS, ParsecKeycode.KEY_EQUALS)
    this.keyCodeMap.set(KeyCode.KEYCODE_LEFT_BRACKET, ParsecKeycode.KEY_LBRACKET)
    this.keyCodeMap.set(KeyCode.KEYCODE_RIGHT_BRACKET, ParsecKeycode.KEY_RBRACKET)
    this.keyCodeMap.set(KeyCode.KEYCODE_BACKSLASH, ParsecKeycode.KEY_BACKSLASH)
    this.keyCodeMap.set(KeyCode.KEYCODE_SEMICOLON, ParsecKeycode.KEY_SEMICOLON)
    this.keyCodeMap.set(KeyCode.KEYCODE_APOSTROPHE, ParsecKeycode.KEY_APOSTROPHE)
    this.keyCodeMap.set(KeyCode.KEYCODE_GRAVE, ParsecKeycode.KEY_BACKTICK)
    this.keyCodeMap.set(KeyCode.KEYCODE_COMMA, ParsecKeycode.KEY_COMMA)
    this.keyCodeMap.set(KeyCode.KEYCODE_PERIOD, ParsecKeycode.KEY_PERIOD)
    this.keyCodeMap.set(KeyCode.KEYCODE_SLASH, ParsecKeycode.KEY_SLASH)
    this.keyCodeMap.set(KeyCode.KEYCODE_CAPS_LOCK, ParsecKeycode.KEY_CAPSLOCK)

    this.keyCodeMap.set(KeyCode.KEYCODE_F1, ParsecKeycode.KEY_F1)
    this.keyCodeMap.set(KeyCode.KEYCODE_F2, ParsecKeycode.KEY_F2)
    this.keyCodeMap.set(KeyCode.KEYCODE_F3, ParsecKeycode.KEY_F3)
    this.keyCodeMap.set(KeyCode.KEYCODE_F4, ParsecKeycode.KEY_F4)
    this.keyCodeMap.set(KeyCode.KEYCODE_F5, ParsecKeycode.KEY_F5)
    this.keyCodeMap.set(KeyCode.KEYCODE_F6, ParsecKeycode.KEY_F6)
    this.keyCodeMap.set(KeyCode.KEYCODE_F7, ParsecKeycode.KEY_F7)
    this.keyCodeMap.set(KeyCode.KEYCODE_F8, ParsecKeycode.KEY_F8)
    this.keyCodeMap.set(KeyCode.KEYCODE_F9, ParsecKeycode.KEY_F9)
    this.keyCodeMap.set(KeyCode.KEYCODE_F10, ParsecKeycode.KEY_F10)
    this.keyCodeMap.set(KeyCode.KEYCODE_F11, ParsecKeycode.KEY_F11)
    this.keyCodeMap.set(KeyCode.KEYCODE_F12, ParsecKeycode.KEY_F12)


    this.keyCodeMap.set(KeyCode.KEYCODE_PRINT, ParsecKeycode.KEY_PRINTSCREEN)
    this.keyCodeMap.set(KeyCode.KEYCODE_SCROLL_LOCK, ParsecKeycode.KEY_SCROLLLOCK)
    this.keyCodeMap.set(KeyCode.KEYCODE_PLAYPAUSE, ParsecKeycode.KEY_PAUSE)
    this.keyCodeMap.set(KeyCode.KEYCODE_INSERT, ParsecKeycode.KEY_INSERT)
    this.keyCodeMap.set(KeyCode.KEYCODE_HOME, ParsecKeycode.KEY_HOME)
    this.keyCodeMap.set(KeyCode.KEYCODE_PAGE_UP, ParsecKeycode.KEY_PAGEUP)
    this.keyCodeMap.set(KeyCode.KEYCODE_DEL, ParsecKeycode.KEY_BACKSPACE)
    this.keyCodeMap.set(KeyCode.KEYCODE_FORWARD_DEL, ParsecKeycode.KEY_DELETE)
    this.keyCodeMap.set(KeyCode.KEYCODE_MOVE_END, ParsecKeycode.KEY_END)
    this.keyCodeMap.set(KeyCode.KEYCODE_PAGE_DOWN, ParsecKeycode.KEY_PAGEDOWN)
    this.keyCodeMap.set(KeyCode.KEYCODE_DPAD_RIGHT, ParsecKeycode.KEY_RIGHT)
    this.keyCodeMap.set(KeyCode.KEYCODE_DPAD_LEFT, ParsecKeycode.KEY_LEFT)
    this.keyCodeMap.set(KeyCode.KEYCODE_DPAD_DOWN, ParsecKeycode.KEY_DOWN)
    this.keyCodeMap.set(KeyCode.KEYCODE_DPAD_UP, ParsecKeycode.KEY_UP)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUM_LOCK, ParsecKeycode.KEY_NUMLOCK)

    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_DIVIDE, ParsecKeycode.KEY_KP_DIVIDE)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_MULTIPLY, ParsecKeycode.KEY_KP_MULTIPLY)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_SUBTRACT, ParsecKeycode.KEY_KP_MINUS)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_ADD, ParsecKeycode.KEY_KP_PLUS)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_ENTER, ParsecKeycode.KEY_KP_ENTER)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_0, ParsecKeycode.KEY_KP_0)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_1, ParsecKeycode.KEY_KP_1)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_2, ParsecKeycode.KEY_KP_2)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_3, ParsecKeycode.KEY_KP_3)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_4, ParsecKeycode.KEY_KP_4)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_5, ParsecKeycode.KEY_KP_5)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_6, ParsecKeycode.KEY_KP_6)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_7, ParsecKeycode.KEY_KP_7)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_8, ParsecKeycode.KEY_KP_8)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_9, ParsecKeycode.KEY_KP_9)
    this.keyCodeMap.set(KeyCode.KEYCODE_NUMPAD_DOT, ParsecKeycode.KEY_KP_PERIOD)

    // this.keyCodeMap.set(KeyCode.KEYCODE_MENU, ParsecKeycode.KEY_APPLICATION)
    this.keyCodeMap.set(KeyCode.KEYCODE_F13, ParsecKeycode.KEY_F13)
    this.keyCodeMap.set(KeyCode.KEYCODE_F14, ParsecKeycode.KEY_F14)
    this.keyCodeMap.set(KeyCode.KEYCODE_F15, ParsecKeycode.KEY_F15)
    this.keyCodeMap.set(KeyCode.KEYCODE_F16, ParsecKeycode.KEY_F16)
    this.keyCodeMap.set(KeyCode.KEYCODE_F17, ParsecKeycode.KEY_F17)
    this.keyCodeMap.set(KeyCode.KEYCODE_F18, ParsecKeycode.KEY_F18)
    this.keyCodeMap.set(KeyCode.KEYCODE_F19, ParsecKeycode.KEY_F19)
    this.keyCodeMap.set(KeyCode.KEYCODE_MENU, ParsecKeycode.KEY_MENU)
    this.keyCodeMap.set(KeyCode.KEYCODE_MUTE, ParsecKeycode.KEY_MUTE)
    this.keyCodeMap.set(KeyCode.KEYCODE_VOLUME_UP, ParsecKeycode.KEY_VOLUMEUP)
    this.keyCodeMap.set(KeyCode.KEYCODE_VOLUME_DOWN, ParsecKeycode.KEY_VOLUMEDOWN)
    this.keyCodeMap.set(KeyCode.KEYCODE_CTRL_LEFT, ParsecKeycode.KEY_LCTRL)
    this.keyCodeMap.set(KeyCode.KEYCODE_SHIFT_LEFT, ParsecKeycode.KEY_LSHIFT)
    this.keyCodeMap.set(KeyCode.KEYCODE_ALT_LEFT, ParsecKeycode.KEY_LALT)
    this.keyCodeMap.set(KeyCode.KEYCODE_META_LEFT, ParsecKeycode.KEY_LGUI)
    this.keyCodeMap.set(KeyCode.KEYCODE_CTRL_RIGHT, ParsecKeycode.KEY_RCTRL)
    this.keyCodeMap.set(KeyCode.KEYCODE_SHIFT_RIGHT, ParsecKeycode.KEY_RSHIFT)
    this.keyCodeMap.set(KeyCode.KEYCODE_ALT_RIGHT, ParsecKeycode.KEY_RALT)
    this.keyCodeMap.set(KeyCode.KEYCODE_META_RIGHT, ParsecKeycode.KEY_RGUI)

    this.keyCodeMap.set(KeyCode.KEYCODE_MEDIA_NEXT, ParsecKeycode.KEY_AUDIONEXT)
    this.keyCodeMap.set(KeyCode.KEYCODE_MEDIA_PREVIOUS, ParsecKeycode.KEY_AUDIOPREV)
    this.keyCodeMap.set(KeyCode.KEYCODE_MEDIA_STOP, ParsecKeycode.KEY_AUDIOSTOP)
    this.keyCodeMap.set(KeyCode.KEYCODE_MEDIA_PLAY_PAUSE, ParsecKeycode.KEY_AUDIOPLAY)
    this.keyCodeMap.set(KeyCode.KEYCODE_VOLUME_MUTE, ParsecKeycode.KEY_AUDIOMUTE)
    // this.keyCodeMap.set(KeyCode.KEYCODE_BUTTON_SELECT, ParsecKeycode.KEY_MEDIASELECT)
  }

  public static getParsecKeycode(keyCode: KeyCode): ParsecKeycode | undefined {
    return CodeMap.instance.keyCodeMap.get(keyCode)
  }
}